package esa.android;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;

public class AbstractAndroidApplication extends Activity {

	protected PrefencesStorage preferences;
	protected GoogleAnalyticsTracker tracker;

	public AbstractAndroidApplication() {
		super();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		tracker = GoogleAnalyticsTracker.getInstance();

		tracker.startNewSession("UA-23581939-1", this);

		preferences = new PrefencesStorage(this.getPreferences(MODE_PRIVATE));

		setContentView(R.layout.main);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		tracker.stopSession();
	}

	@Override
	protected void onStart() {
		super.onStart();
		
		startApplication();
		
		tracker.trackPageView("/startApplication");
		tracker.dispatch();
	}

	protected void startApplication() {

	}

	@Override
	protected void onStop() {
		super.onStop();
		preferences.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);

		return true;
	}

	protected String getApplicationVersionInfo() {
		PackageInfo info;
		try {
			info = getPackageManager().getPackageInfo(getPackageName(), 0);
			String name = info.applicationInfo.loadLabel(getPackageManager()).toString();
			return name + " " + info.versionName + " (" + info.versionCode + ")\nhttp://" + getResources().getString(R.string.title);

		} catch (NameNotFoundException e) {
		}
		return "";
	}

}